package com.invest.vo;

import com.google.common.collect.Lists;
import com.invest.domain.model.SystemUserDO;
import com.invest.domain.model.SystemUserRolesDO;
import com.invest.infra.constants.AppConstants;
import com.invest.utils.type.SystemUserStatusType;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * <p>注释</p>
 *
 * @author yuyizhao
 * @version v0.1 2016/6/14 Exp
 */
public class UserVO {

    @NotBlank(message = "用户ID不能为空")
    private String id;

    @NotBlank(message = "名称不能为空")
    private String name;

    @NotBlank(message = "密码不能为空")
    private String password;

    @Size(min = 1,message = "角色不能为空")
    private String[] roles;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getRoles() {
        return roles;
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }

    public SystemUserDO populate(boolean isAdd, String operator){
        SystemUserDO admin = new SystemUserDO();
        admin.setId(getId());
        admin.setName(getName());
        if(isAdd){
            admin.setStatus(SystemUserStatusType.NORMAL.name());
            if(StringUtils.isBlank(getPassword())) {
                admin.setPassword(AppConstants.DEFAULT_PASSWORD);
            }
            admin.setCreateUser(operator);
            admin.setCreateTime(new Date());
            admin.setPassword(getPassword());
        }
        admin.setUpdateUser(operator);
        admin.setUpdateTime(new Date());
        return admin;
    }
    public List<SystemUserRolesDO> populateRoles(boolean isAdd, String operator){

        if(ArrayUtils.isEmpty(roles)){
            return null;
        }
        List<SystemUserRolesDO> results = Lists.newArrayList();
        for(String item : roles) {
            SystemUserRolesDO roles = new SystemUserRolesDO();
            roles.setRole(item);
            roles.setUserId(getId());
            if(isAdd){
                roles.setCreateUser(operator);
                roles.setCreateTime(new Date());
            }
            roles.setUpdateUser(operator);
            roles.setUpdateTime(new Date());
            results.add(roles);
        }
        return results;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
